package club.menglinking.wechat.util;

import com.aliyun.oss.OSSClient;

import java.io.File;

/**
 * @ClassName AliyunOssUtil
 * @Author xwu36
 * @Date 2019/1/7 13:25
 * @Version 1.0
 * @Description
 **/
public class AliyunOssUtil {
    private static AliyunOssUtil ossUtil = null;
    private AliyunOssUtil() {
    }
    public static AliyunOssUtil getOssUtil(){
        if (ossUtil==null){
            ossUtil=new AliyunOssUtil();
        }
        return ossUtil;
    }
    /**
     *@MethodName fileRename
     *@Data 2019/1/7 13:33
     *@Author wuxuan
     *@Description 文件重命名,源文件名+日期
     **/
    public String fileRename(String fileName){
        String[] strings = BaseUtil.getBaseUtil().getStringSplit(fileName, ".");
        return strings[0]+"-"+BaseUtil.getBaseUtil().getIntegerTime()+"."+strings[1];
    }
    /**
     *@MethodName getOSSClient
     *@Data 2019/1/7 13:44
     *@Author wuxuan
     *@Description 建立连接
     **/
    public OSSClient getOSSClient(String ossAddress, String ossKey, String OssScret){
        OSSClient ossClient=new OSSClient(ossAddress,ossKey,OssScret);
        return ossClient;
    }
    /**
     *@MethodName uploadByFile
     *@Data 2019/1/7 13:47
     *@Author wuxuan
     *@Description 通过本地文件上传 file 本地文件 bucketName 存储桶的名字 objectName 上传的文件包括目录
     **/
    public boolean uploadByFile(OSSClient ossClient, File file, String bucketName, String objectName) {
        boolean isSUccess=true;
        try {
            ossClient.putObject(bucketName, objectName, file);
        } catch (Exception e) {
            isSUccess=false;
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return isSUccess;
    }

}
